package viewcontroller;

import java.util.ArrayList;

import utils.GridViewItem;
import utils.ServiceHandler;

import model.Store;
import com.example.lastprojectbeautiplus.R;
import com.google.gson.Gson;
import com.squareup.picasso.Picasso;

import adapter.HomeGridViewAdapter;
import android.app.Fragment;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;

public class ManageStoreFragment extends Fragment {
	public ManageStoreFragment() {
	}

	// Define
	Store[] store;
	// ArrayList<Store> storeList;
	ArrayList<GridViewItem> controlList;
	ArrayList<GridViewItem> statisticList;
	GridView gvControlList;
	GridView gvStatisticList;
	HomeGridViewAdapter gridviewAdapter;
	HomeGridViewAdapter gvAdapter;
	ImageView imgBanner, imgLogo;
	int height, width;
	SQLiteDatabase dbAccount;
	Cursor csAccount;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View rootView = inflater.inflate(R.layout.fragment_manage_store,
				container, false);
		DisplayMetrics metrics = rootView.getResources().getDisplayMetrics();
		width = metrics.widthPixels;
		height = metrics.heightPixels;
		controlList = new ArrayList<GridViewItem>();
		statisticList = new ArrayList<GridViewItem>();
		imgBanner = (ImageView) rootView.findViewById(R.id.imgStoreBanner);
		imgLogo = (ImageView) rootView.findViewById(R.id.imgStoreLogo);
		imgBanner.getLayoutParams().width = width;
		imgBanner.getLayoutParams().height = height / 3;
		imgLogo.getLayoutParams().width = width / 5;
		imgLogo.getLayoutParams().height = width / 5;
		createAccountTable();
		AddItemToGridViewControl();
		AddItemtoGridViewStatistic();
		csAccount = dbAccount.rawQuery("SELECT * FROM ThisAccount", null);
		csAccount.moveToFirst();
		// Call Store info
		String urlStoreView = "http://services-beautyplus.rhcloud.com/rest/StoreServices/StoreByUser/"
				+ csAccount.getInt(csAccount.getColumnIndex("userID"));
		new GetStore(urlStoreView).execute();
		// MakeGridview
		gvControlList = (GridView) rootView.findViewById(R.id.gvControlList);
		gvStatisticList = (GridView) rootView
				.findViewById(R.id.gvStatisticList);
		gridviewAdapter = new HomeGridViewAdapter(getActivity(),
				R.layout.gridview_item, controlList);
		gvControlList.setAdapter(gridviewAdapter);
		gvControlList.getLayoutParams().height = (int) (height / 4.5);
		gvControlList.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				if (controlList.get(position).getTarget() == 1) {
					Intent iManageProuct = new Intent(getActivity(),
							SlideMenu.class);
					iManageProuct.putExtra("FromManageProduct", csAccount
							.getInt(csAccount.getColumnIndex("userID")));
					getActivity().startActivity(iManageProuct);
				} else if (controlList.get(position).getTarget() == 2) {

				}
			}
		});
		gvAdapter = new HomeGridViewAdapter(getActivity(),
				R.layout.gridview_item, statisticList);
		gvStatisticList.setAdapter(gvAdapter);
		return rootView;
	}

	private class GetStore extends AsyncTask<Void, Void, Void> {
		String url;
		String jsonRespone;

		public GetStore(String url) {
			this.url = url;
		}

		@Override
		protected Void doInBackground(Void... params) {
			// TODO Auto-generated method stub
			ServiceHandler sh = new ServiceHandler();
			jsonRespone = sh.executeGetRequest(url);
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			// TODO Auto-generated method stub
			Gson myGson = new Gson();
			store = myGson.fromJson(jsonRespone, Store[].class);
			String bannerPath = store[0].getBanner_path().toString();
			String logoPath = store[0].getLogo_path().toString();

			Picasso.with(getActivity()).load(bannerPath)
					.placeholder(R.drawable.image_wait)
					.error(R.drawable.error_loading).into(imgBanner);

			Picasso.with(getActivity()).load(logoPath)
					.placeholder(R.drawable.image_wait)
					.error(R.drawable.error_loading).into(imgLogo);

			super.onPostExecute(result);
		}

	}

	private void AddItemToGridViewControl() {
		controlList.add(new GridViewItem("Quản lý sản phẩm",
				R.drawable.banner_setclothing, 1));
		controlList.add(new GridViewItem("Quản lý thông tin cửa hàng",
				R.drawable.manage_information, 2));
	}

	private void AddItemtoGridViewStatistic() {
		statisticList.add(new GridViewItem("Thống kê sản phẩm bán chạy",
				R.drawable.statistic_best_sell, 1));
		statisticList.add(new GridViewItem("Thống kê các giao dịch",
				R.drawable.statistic_trade, 2));
		statisticList.add(new GridViewItem("Thông báo",
				R.drawable.statistic_note, 3));
	}

	private void createAccountTable() {

		dbAccount = getActivity().openOrCreateDatabase("ThisAccount",
				android.content.Context.MODE_PRIVATE, null);
		String sqlAccount = "CREATE TABLE IF NOT EXISTS ThisAccount(userID INT, storeOwner INT);";
		dbAccount.execSQL(sqlAccount);
	}
}
